// pages/company-detail/comDetail.js
import Ajax from '../../common/Ajax';
import citys from '../../common/citys';

let sessionId = wx.getStorageSync('sessionId');

Page({

    /**
     * 页面的初始数据
     */
    data: {
        company: "",
        leagle: "",
        phone: "",
        location: "",
        office: "",
        locationShow: false,
        officeShow: false,
        areaList: citys,
        locaVal: "110101",
        officeCode: "",
        locationCode: "",
        companyInfo: {},
        sign: "",
        userCompanyId: "",
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            sign: options && options.sign,
            userCompanyId: options && options.userCompanyId,
        })
        if(!options.sign) {
            wx.showLoading({
                title: '查询中...',
            })
            this.setData({})
            Ajax.get(`/userCompanys/findUserCompanyById?userCompanyId=${options.userCompanyId}`, {}, sessionId)
            .then(res => {
                if(res.code === "0") {
                   this.setData({companyInfo: res.data})
                } else {
                    Notify(res.message)
                }
            })
            .catch(err => {
                throw err;
            })
            .finally(() => {
                wx.hideLoading()
            })
        }
    },

    onChange: function(e) {
        const { id } = e.currentTarget;
        let val = e.detail;
        let {companyInfo} = this.data;
        if(id === "company") {
            companyInfo.companyName = val
            this.setData({company: val, companyInfo})
        } else if(id === "leagle") {
            companyInfo.legalPerson = val
            this.setData({leagle: val, companyInfo})
        } else if(id === "phone") {
            companyInfo.phoneNo = val
            this.setData({phone: val, companyInfo})
        } else if(id === "work") {
            companyInfo.workAddressInfo = val
            this.setData({workAddressInfo: val, companyInfo})
        } else if(id === "register") {
            companyInfo.registerAddressInfo = val
            this.setData({registerAddressInfo: val, companyInfo})
        }
    },

    chooseLoc: function(e) {
        const { id } = e.currentTarget;
        if(id === "loca") {
            this.setData({locationShow: true})
        } else if(id === "office") {
            this.setData({officeShow: true})
        }
    },
    /**
     * 
     * 拼接地点函数
     */
    joinLoca: function (e) {
        let completeName = "";
        let param = e.detail.values;
        param.forEach(item => {
            completeName += `${item.name} `
        })
        return completeName;
    },

    setVal: function(e) {
        const { id } = e.currentTarget;
        const result = this.joinLoca(e);
        let { companyInfo } = this.data;
        if(id === "loca") {
            companyInfo.registerCounty = e.detail.values[2].code
            this.setData({
                location: result,
                locationCode: e.detail.values[2].code,
                locationShow: false,
                companyInfo,
            })
        } else if(id === "office") {
            companyInfo.workCounty = e.detail.values[2].code
            this.setData({
                office: result,
                officeCode: e.detail.values[2].code,
                officeShow: false,
                companyInfo,
            })
        }
    },

    toSave: function() {
        const { 
            company, 
            leagle, 
            phone, 
            officeCode, 
            locationCode, 
            workAddressInfo, 
            registerAddressInfo, 
            sign,
            companyInfo,
         } = this.data;
        wx.showLoading({
          title: '进行中....',
        })
        const param = {
            companyName: company,
            legalPerson: leagle,
            phoneNo: phone,
            registerCounty: locationCode,
            workCounty: officeCode,
            workAddressInfo,
            registerAddressInfo,
        }
        if(sign === "add") {
            Ajax.post("/userCompanys/addCompany", param, sessionId)
                .then(res => {
                    if(res.code === "0") {
                        wx.showToast({
                        title: '添加成功～',
                        })
                        wx.navigateTo({
                        url: '../my-company/myCompany',
                        })
                    } else {
                        Notify(res.message)
                    }
                })
                .catch(err => {
                    throw err;
                })
                .finally(() => {
                    wx.hideLoading()
                })
        } else {
            Ajax.post("/userCompanys/updateCompany", companyInfo, sessionId)
                .then(res => {
                    if(res.code === "0") {
                        wx.showToast({
                        title: '修改成功～',
                        })
                        wx.navigateTo({
                        url: '../my-company/myCompany',
                        })
                    } else {
                        Notify(res.message)
                    }
                })
                .catch(err => {
                    throw err;
                })
                .finally(() => {
                    wx.hideLoading()
                })
        }
    }
})